package com.yf.dxapp.module.app.controller.app;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import com.yf.dxapp.module.app.component.app.AppMaterialComponent;
import com.yf.dxapp.module.app.controller.admin.category.vo.CategoryPageReqVO;
import com.yf.dxapp.module.app.controller.app.vo.ClassificationVo;
import com.yf.dxapp.module.app.controller.app.vo.CommonRequest;
import com.yf.dxapp.module.app.controller.app.vo.AppMaterialVO;
import com.yf.dxapp.module.app.controller.app.vo.ListResult;
import com.yf.dxapp.module.app.controller.app.vo.ProductListRequest;
import com.yf.dxapp.module.app.dal.dataobject.category.CategoryDO;
import com.yf.dxapp.module.app.enums.app.AppCategoryTypeEnum;
import com.yf.dxapp.module.app.service.category.CategoryService;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * @author yxmds
 */
@RestController
@RequestMapping("/material")
public class ApiAppMaterialController extends ApiAppBaseController {

    @Resource
    private AppMaterialComponent appMaterialComponent;

    @Resource
    private CategoryService categoryService;

    @RequestMapping(value = "/type",
            method = RequestMethod.POST)
    @PermitAll
    public CommonResult appCourseClassification() {
        CategoryPageReqVO query = new CategoryPageReqVO();
        query.setParentType(AppCategoryTypeEnum.MATERIAL_TAB_ROOT.getType());
        PageResult<CategoryDO> result = categoryService.getCategoryPage(query);
        return success(result.getList().stream().map(ClassificationVo::build).collect(Collectors.toList()));
    }


    @RequestMapping(value = "/list",
            method = RequestMethod.POST)
    @PermitAll
    public CommonResult getList(@RequestBody(required = true) ProductListRequest request) {
        ListResult<AppMaterialVO> kolProductList = appMaterialComponent.getKolProductList(null, request);
        return success(kolProductList);
    }

    @RequestMapping(value = "/detail",
            method = RequestMethod.POST)
    public CommonResult getDetail(@RequestBody CommonRequest request) {
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        AppMaterialVO appMaterialVO = appMaterialComponent.getDetail(loginUserId, request.getPid());
        return success(appMaterialVO);
    }

}
